<?php
namespace app\common\model;

use think\Model;

class Admin extends Model
{
    protected $pk     = 'id';
    protected $insert = ['allow_del' => 1,'create_time'];

    protected function setCreateTimeAttr()
    {
        return time();
    }

    protected function setPwdAttr($val)
    {
        return  sha1md5($val);
    }



    public function getAdmins($keyword, $page = 20, $order = "id desc")
    {
        if($keyword){
             $list = $this::where("tel|name","like","%".$keyword."%")->order($order)->paginate($page);
         }else{
             $list = $this::order($order)->paginate($page);
         }
        $data = $list->toArray();
        return $data;
    }

    public function getAdmin($where)
    {
        return $this::where($where)->find();
    }

    public function saveAdmin($data, $where = false)
    {
        if ($where) {
            $this::where($where)->update($data);
        } else {
            if ($this::save($data)) {
                return $this->id;
            }
        }
    }

    public function delAdmin($id)
    {
        if ($id) {
            $where['id'] = ["in", $id];
            return $this::where($where)->where(["allow_del" => 1])->delete();
        }
    }
}
